package com.dd.ljzapplication.activity;

import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.dd.ljzapplication.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import base.BaseUtilActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by LiJZ on 2016/12/5.
 */

public class UpdateVersionActivity extends BaseUtilActivity {

	private TextView mTextView;
	private int version;

	@Override
	protected int setLayoutID() {
		return R.layout.activity_update_version;
	}

	@Override
	protected void init() {

	}

	@Override
	protected void initActionBar() {

	}

	@Override
	protected void initView() {
		mTextView = (TextView) findViewById(R.id.tv);
	}

	@Override
	protected void initEvent() {

	}

	public void click(View view) {
		OkHttpClient client = new OkHttpClient.Builder()
				.build();
		String url = "http://192.168.56.1:8080/version.json";
		Request request = new Request.Builder().url(url).get().build();
		Call call = client.newCall(request);
		call.enqueue(new Callback() {
			@Override
			public void onFailure(Call call, IOException e) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mTextView.setText("faile");
					}
				});
				Log.i("###", "onFailure: ");
			}

			@Override
			public void onResponse(Call call, Response response) throws IOException {
				final String string = response.body().string();
				try {
					JSONObject object = new JSONObject(string);
					version = object.optInt("version");
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							mTextView.append(string+"\n");
							mTextView.append("版本号："+ version);
						}
					});
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		});

	}
}
